//
//  LoginCodeSendController.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/1/20.
//  Copyright © 2018年 smallyou. All rights reserved.
//


//controller
#import "LoginCodeSendController.h"
#import "PerfectInfoController.h"
#import "SuccessToOriginController.h"

//view
#import "KCountdownButton.h"

typedef NS_ENUM(NSUInteger, LoginCodeSendType) {
    LoginCodeSendTypeRegister,
    LoginCodeSendTypeResetPwd,
};

@interface LoginCodeSendController ()<KCountdownButtonDelegate> {
    LoginCodeSendType VCType_;
}
//输入框高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputViewH;
//手机号
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
//区号
@property (weak, nonatomic) IBOutlet UIButton *areaCodeBtn;
//密码
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
//查看密码
@property (weak, nonatomic) IBOutlet UIButton *secureButton;
//完善资料
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
//验证码
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
//获取验证码按钮
@property (weak, nonatomic) IBOutlet KCountdownButton *getCodeBtn;
//重复密码
@property (weak, nonatomic) IBOutlet UITextField *againPWDTextField;
//查看重复密码
@property (weak, nonatomic) IBOutlet UIButton *againSecureBtn;

@end

@implementation LoginCodeSendController

- (instancetype)initWithTypeRegister {
    return [self initWithType:LoginCodeSendTypeRegister];
}
- (instancetype)initWithTypeResetPwd {
    return [self initWithType:LoginCodeSendTypeResetPwd];
}

- (instancetype)initWithType:(LoginCodeSendType)type {
    if (self = [super init]) {
        VCType_ = type;
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.secureButton.app_touchExtendInset = UIEdgeInsetsMake(-10, -10, -10, -10);
    self.againSecureBtn.app_touchExtendInset = UIEdgeInsetsMake(-10, -10, -10, -10);
    switch (VCType_) {
        case LoginCodeSendTypeRegister: self.title = @"手机号码注册";
            [self.confirmButton setTitle:@"完善资料" forState:UIControlStateNormal]; break;
        case LoginCodeSendTypeResetPwd: self.title = @"忘记密码";[self.confirmButton setTitle:@"确认找回" forState:UIControlStateNormal]; break;
    }
    
    //设置倒数按钮
    self.getCodeBtn.delegate = self;

    [self p_updateInputUIAutoLayot];
    
    NSArray *btns = @[self.areaCodeBtn,self.secureButton,self.againSecureBtn, self.confirmButton];
    
    for (UIButton *btn in btns) {
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - Action
- (void)btnDidClick:(UIButton *)sender {
    //选择区号
    if (sender == self.areaCodeBtn) {

            //查看密码
    }else if (sender == self.secureButton) {
        sender.selected = !sender.isSelected;
        self.pwdTextField.secureTextEntry = !sender.isSelected;
        
        //查看再次输入的密码
    }else if(sender == self.againSecureBtn){
        sender.selected = !sender.isSelected;
        self.againPWDTextField.secureTextEntry = !sender.isSelected;
        
        //完善资料
    }else if(sender == self.confirmButton){
        switch (VCType_) {
            case LoginCodeSendTypeRegister: {
                NSString *validate = [self p_valiteIsCanRegister];
                if (validate.length != 0) {
                    [KLProgressHUD showPromptHUDWithMessage:validate];
                    return;
                }
                
                MJWeakSelf
                [KLProgressHUD showWaitingHUD];
                [AppNetwork checkRegisterCodeWithPhone:self.phoneTextField.text smsCode:self.codeTextField.text Completion:^(BOOL success, id responseStr, NSString *errorMsg) {
                    [KLProgressHUD hideHUDWithAnimated:YES];
                    if (success) {
                        
                        NSLog(@"------>去完善资料");
                        PerfectInfoController *perfet = [[PerfectInfoController alloc] init];
                        perfet.phone = weakSelf.phoneTextField.text;
                        perfet.pwd = weakSelf.pwdTextField.text;
                        perfet.smsCode = weakSelf.codeTextField.text;
                        [weakSelf.navigationController pushViewController:perfet animated:YES];
                        
                    }else{
                        [KLProgressHUD showErrorHUDWithMessage:errorMsg];
                    }
                }];
                

            }
                break;
                
            case LoginCodeSendTypeResetPwd: {
                NSString *validate = [self validateIsCanResetPwd];
                if (validate.length != 0) {
                    [KLProgressHUD showPromptHUDWithMessage:validate];
                    return;
                }
                
                NSLog(@"------>确认重置密码");
                [self request_resetPwd];
            }
                break;
        }
    }
}

//发送验证码
-(void)countdownButtonClick{
    
    NSString *isCanSendCode = [self validateIsCanSendCode];
    if (isCanSendCode.length != 0) {
        [self.view endEditing:YES];
        [KLProgressHUD showPromptHUDWithMessage:isCanSendCode];
        return;
    }
    [self request_sendCode];
    
}

#pragma mark - Request
// 发送验证码请求
- (void)request_sendCode {
    
    MJWeakSelf
    [AppHttpClient getCodeWithPhone:self.phoneTextField.text completion:^(BOOL success, NSString *errorMsg) {
        //

        if (!success) {
            [KLProgressHUD showErrorHUDWithMessage:errorMsg];
            return;
        }
        
        [weakSelf.getCodeBtn startCountdown];
        [KLProgressHUD showPromptHUDWithSuperView:nil message:@"短信已发送" completed:^{
        }];
    }];
}


#pragma mark - Request
//重置密码
- (void)request_resetPwd {
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:@"重置中.."];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"passWord"] = [NSString app_md5:self.pwdTextField.text];
    param[@"phone"] = self.phoneTextField.text;
    param[@"smsCode"] = self.codeTextField.text;
    
    [AppNetwork postWithUrl:kResetPwdUrl params:param success:^(id response) {
        progressHUD.hidden = YES;
        
        [AppNotificationHelpper postNotificationName:@"_clearAccountAndPwd_" object:nil];
        
        SuccessToOriginController *success = [[SuccessToOriginController alloc] init];
        [self.navigationController pushViewController:success animated:YES];
        
        WEAKSELF
        [success setNavTitle:@"找回密码" desTitle:@"重置密码成功" btnTitle:@"返回登录"];
        [success setBtnClickHandle:^(SuccessToOriginController *btnVC) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }];
        
    } fail:^(AppError *error) {
        progressHUD.hidden = YES;
        [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
    }];
}



#pragma mark - Private

- (void)p_updateInputUIAutoLayot {
    if (VCType_ != LoginCodeSendTypeResetPwd) {
        return;
    }
    
    self.inputViewH.constant = 150;
}

- (NSString *)p_valiteIsCanRegister {
    switch (VCType_) {
        case LoginCodeSendTypeRegister: {
            if ([NSString app_isBlankString:self.phoneTextField.text]) {
                return @"请输入手机号";
            }
            if ([NSString app_isBlankString:self.pwdTextField.text]) {
                return @"请输入密码";
            }
            if (self.pwdTextField.text.length > 16 || self.pwdTextField.text.length < 6) {
                return @"请输入6-16位密码";
            }
            
            if (![self.pwdTextField.text isEqualToString:self.againPWDTextField.text]) {
                return @"两次密码输入不一致";
            }
            
            if (![self.codeTextField.text isNotBlank]) {
                return @"请输入验证码";
            }
            return nil;
        }
            break;
            
        case LoginCodeSendTypeResetPwd:{
            if ([NSString app_isBlankString:self.phoneTextField.text]) {
                return @"请输入手机号";
            }
            return nil;
        }
            break;
    }
}


//检测能否发送验证码
- (NSString *)validateIsCanSendCode {
    if ([NSString app_isBlankString:self.phoneTextField.text]) {
        return @"请输入手机号码";
    }
    
    return nil;
}
//检测能否重置密码
- (NSString *)validateIsCanResetPwd {
    if ([NSString app_isBlankString:self.phoneTextField.text]) {
        return @"请输入手机号码";
    }
    
    if ([NSString app_isBlankString:self.codeTextField.text]) {
        return @"请输入手机验证码";
    }
    
    if ([NSString app_isBlankString:self.pwdTextField.text]) {
        return @"请输入新密码";
    }
    
    if (self.pwdTextField.text.length < 6 || self.pwdTextField.text.length > 16) {
        return @"请输入6-16位新密码";
    }
    
    return nil;
}



@end
